/*
* Copyright 2010 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/

package org.etherframework.wire
{
	import org.etherframework.core.Access;
	import org.etherframework.core.DomainAccess;
	import org.etherframework.reflection.MetadataProperty;
	
	import flash.events.EventPhase;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import flash.system.ApplicationDomain;

	public class WiringUtil
	{
		private static var resolveCache:Dictionary = new Dictionary(true);
		
		public static function getProperties(items:Array):Dictionary
		{
			var result:Dictionary = new Dictionary();
			var itemsLen:int = items.length;
			var item:MetadataProperty;
			
			for (var i:int = 0; i < itemsLen; ++i)
			{
				item = items[i];
				
				if (item.key)
				{
					result[item.key] = item.value;
				}
				else
				{
					result["__value"] = item.value;
				}
			}
			return result;
		}
		
		public static function resolveName(resolvers:Array, name:String, domain:ApplicationDomain):String
		{
			if (resolvers.length)
			{
				var cache:Dictionary = resolveCache[resolvers] || 
					(resolveCache[resolvers] = new Dictionary()); 
				
				if (cache[name])
					return cache[name];
				
				var result:String = name;
				var chain:Array;
				var resolverName:String;
				var resolverProperty:String;
				var resolver:Object;
				var target:Object;
				var targetName:String;
				var max:int = resolvers.length;
				
				if (name.indexOf(".") > 0)
				{
					chain = name.split(".");
					resolverName = chain[0];
					resolverProperty = chain[1];
				}
				else
				{
					resolverProperty = name;
				}
				
				for (var i:int = 0; i < max; ++i)
				{
					target = (resolvers[i] is String) ? (domain.hasDefinition(resolvers[i]) ? 
								domain.getDefinition(resolvers[i]) : null) : resolvers[i];
					
					if (!target)
						continue
						
					targetName = getQualifiedClassName(target).split("::")[1];
					
					if (resolverName)
					{
						if ((targetName == resolverName) && (resolverProperty in target))
						{
							result = (target[resolverProperty] is String) ? 
										target[resolverProperty] : resolverProperty;
							break;
						}
					}
					else if (resolverProperty in target)
					{
						result = (target[resolverProperty] is String) ?
									target[resolverProperty] : resolverProperty;
						break;
					}
				}
				
				cache[name] = result;
				return result;
			}
			
			return name;
		}
		
	}
}